Optional Text.f

{
TODO:
   figure out what words we'll be using for what to draw text


   word wrap - display until next word would overflow a given width then do a (GCR)
   centered text with word wrap
   justified text with word wrap
   recognize line feeds
}

Requires Tools
Requires SORA\Transformations
Requires SORA\Sprites
Requires SORA\Vertices
Requires SORA\Images.f

Only Forth Definitions

image extend: font
   int field firstchar
end-type

0 font.ref fnt

Vertex2D GL_QUADS vbuf.make text-vbuf

0 Value text-count

transform.make text-transform


$ffffffff text-transform.color.!
1.0 1.0 text-transform.scale.2!
0.0 text-transform.rotation.z.!

\ verbose

method: font.char[]  ( n fnt -- sprite )
   firstchar.@ - subimages.[] ;

: font.height ( fnt -- n )
   0 subimages.[].h.@ ;

-? 0 transform.ref t
-? 0 subimage.ref s
: draw-char ( subimage transform -- )
   to t to s
   t s.fast-sprite  s.xstride.@  text-transform.pos.x.+! ;

: (gemit)  ( c -- )
   fnt.char[] text-transform draw-char
   1 +to text-count ;

: (gtype) ( addr c -- )
   0 ?do dup c@ (gemit) 1+ loop drop ;

: (gcr) ( -- )
   text-transform.pos.x.clear  fnt.height  text-transform.pos.y.+! ;

Create gpersonality
   4 cells ,
   7 ,
   0 ,
   0 ,
   ' noop ,
   ' noop ,
   ' noop ,
   ' (gemit) ,
   ' (gtype) ,
   ' (gtype) ,
   ' (gcr) ,

: font.bind
   dup super.bind
   to fnt
   text-transform.color.@ to vcolor
   0 to text-count ;


-? 0 value xt
: text-vertices  ( count vbuf xt -- ) ( -- )
   to xt
   gpersonality open-personality
   vbuf.define:[ xt execute ;]
   close-personality ;

:[] draw-text:[ ( fnt xt -- ) ( -- )
   0 0  text-transform.pos.2!
   swap font.bind 65536 text-vbuf rot text-vertices
   text-vbuf 0 text-count 4 * vbuf.draw-some ;

: init-text-display  ( -- )
   ;



0 font.ref fnt'
: font.make ( imagefile count fntfile count -- <name> )
   2swap Graphic:     lastbody @ to fnt'
   font sizeof image sizeof - /allot
   here >r cache-file, r> cache-count drop
      h@+ fnt'.firstchar.!
      h@+ drop \ fnt ->last-char !
      fnt'.subimages.! ;


